﻿using System;
using ICSharpCode.SharpZipLib.Zip;
using System.Windows.Forms;
using System.IO;

namespace IPBoss.Copyright.WindowsUpdater
{
    public class ZipHelper
    {
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="URL">下载文件地址</param>
        /// <param name="Filename">下载后的存放地址</param>
        /// <param name="Prog">用于显示的进度条</param>
        public static void DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog)
        {
            try
            {
                progressBar1 = prog;

                System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
                System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
                long totalBytes = myrp.ContentLength;

                if (prog != null)
                {
                    SetprogressResult(totalBytes.ToString(), 2);
                    //prog.Maximum = (int)totalBytes;
                }
                System.IO.Stream st = myrp.GetResponseStream();
                System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
                long totalDownloadedByte = 0;
                byte[] by = new byte[1024];
                int osize = st.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    System.Windows.Forms.Application.DoEvents();
                    so.Write(by, 0, osize);

                    if (prog != null)
                    {
                        SetprogressResult((totalDownloadedByte - ((int)(totalDownloadedByte * 0.1))).ToString(), 1);
                        //prog.Value = (int)(totalDownloadedByte - (totalDownloadedByte * 0.1));
                    }
                    osize = st.Read(by, 0, (int)by.Length);
                }
                so.Close();
                st.Close();
            }
            catch (System.Exception)
            {
                throw;
            }
        }

        public static ProgressBar progressBar1;
        public delegate void SetlabTextHandler(string result, int type);
        /// <summary>
        /// 更改 进度条
        /// </summary>
        /// <param name="result"></param>
        private static void SetprogressResult(string result, int type)
        {
            if (progressBar1.InvokeRequired == true)
            {

                SetlabTextHandler set = new SetlabTextHandler(SetprogressResult);//委托的方法参数应和SetCalResult一致  
                progressBar1.Invoke(set, new object[] { result, type }); //此方法第二参数用于传入方法,代替形参result  

            }
            else
            {
                if (type == 1)
                {
                    progressBar1.Value = int.Parse(result);
                }
                else
                {
                    progressBar1.Maximum = int.Parse(result);
                }
            }
        }


        /// <summary>
        /// 循环解压文件
        /// </summary>
        /// <param name="zipFileName"></param>
        /// <param name="unZipPath"></param>
        /// <returns></returns>
        public static string unZipFile(string zipFileName, string unZipPath)
        {
            string rootFile = " ";
            try
            {
                //读取压缩文件(zip文件)，准备解压缩
                ZipInputStream s = new ZipInputStream(File.OpenRead(zipFileName.Trim()));
                ZipEntry theEntry;
                string path = unZipPath;
                //解压出来的文件保存的路径
                string rootDir = " ";
                //根目录下的第一个子文件夹的名称
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    rootDir = Path.GetDirectoryName(theEntry.Name);
                    //得到根目录下的第一级子文件夹的名称
                    if (rootDir.IndexOf("\\") >= 0)
                    {
                        rootDir = rootDir.Substring(0, rootDir.IndexOf("\\") + 1);
                    }
                    string dir = Path.GetDirectoryName(theEntry.Name);
                    //根目录下的第一级子文件夹的下的文件夹的名称
                    string fileName = Path.GetFileName(theEntry.Name);
                    //根目录下的文件名称
                    if (dir != " ")
                    //创建根目录下的子文件夹,不限制级别
                    {
                        if (!Directory.Exists(unZipPath + "\\" + dir))
                        {
                            path = unZipPath + "\\" + dir;
                            //在指定的路径创建文件夹
                            Directory.CreateDirectory(path);
                        }
                    }
                    else if (dir == " " && fileName != "")
                    //根目录下的文件
                    {
                        path = unZipPath;
                        rootFile = fileName;
                    }
                    else if (dir != " " && fileName != "")
                    //根目录下的第一级子文件夹下的文件
                    {
                        if (dir.IndexOf("\\") > 0)
                        //指定文件保存的路径
                        {
                            path = unZipPath + "\\" + dir;
                        }
                    }
                    if (dir == rootDir)
                    //判断是不是需要保存在根目录下的文件
                    {
                        path = unZipPath + "\\" + rootDir;
                    }
                    //以下为解压缩zip文件的基本步骤
                    //基本思路就是遍历压缩文件里的所有文件，创建一个相同的文件。
                    if (fileName != String.Empty)
                    {
                        FileStream streamWriter = File.Create(path + "\\" + fileName);
                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                        streamWriter.Close();
                    }
                }
                s.Close();
                return rootFile;
            }
            catch (Exception ex)
            {
                return "1; " + ex.Message;
            }
        }

    }
}
